using System;
using System.Collections.Generic;
using System.Windows.Forms;
using AssetManagementSystem.Utils;
using AssetManagementSystem.Utils.Events;

namespace AssetManagementSystem.View.Controls {
    public partial class UIPropertyEditor : Form {

        public EventHandler Save_Clicked;
        public const string EDIT = "Edit";
        public const string NEW = "New";
        public const string NAME = "NAME";
        public const string VALUE = "VALUE";
        public const string TYPE_KEY = "Property";
        string type;

        public UIPropertyEditor() {
            InitializeComponent();
            setTitle(NEW + " " + TYPE_KEY);
            type = NEW;
        }

        public UIPropertyEditor(string name, string value) {
            InitializeComponent();
            setTitle(EDIT + " " + TYPE_KEY);
            txtName.Text = name;
            //Existing property names cannot be edited.
            txtName.Enabled = false;
            txtValue.Text = value;
            type = EDIT;
        }

        private void UIPropertyEditor_Load(object sender, EventArgs e) {
        }

        //sets the title of the form, and the title label's value.
        public void setTitle(string title) {
            this.Text = title;
            this.lblTitle.Text = title;
        }

        private void btnSave_Click(object sender, EventArgs e) {
            if (txtName.Text == TYPE_KEY) {
                MessageBox.Show("The property's name cannot be '" + TYPE_KEY + "', it is a reserved name.");
            } else {
                Dictionary<string, string> args = new Dictionary<string, string>();
                args.Add(NAME, txtName.Text);
                args.Add(VALUE, txtValue.Text);
                //set the update new (i.e new or edit)
                args.Add(TYPE_KEY, this.type);
                Save_Clicked.Invoke(this, new DictionaryEventArgs(args));
            }
        }

        //if cancel is clicked, close the form
        private void btnCancel_Click(object sender, EventArgs e) {
            this.Close();
        }
    }
}
